package com.itheima.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

public class CookieUtils {

    /*
        根据名称查找指定Cookie对象
     */
    public static Cookie findCookieByName(HttpServletRequest request, String cookieName) {
        //1. 获取所有Cookie的数组
        Cookie[] cookies = request.getCookies();

        //2. 遍历数组, 根据名称查找指定Cookie对象
        if (cookies != null) {
            for (Cookie c : cookies) {
                if (cookieName.equals(c.getName())) {
                    //找到了
                    return c;
                }
            }
        }

        // 找不到, 返回null
        return null;

    }
}
